package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取秒杀开始的时间内的商品信息
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getSeckillTime() {
       //开始时间
        String start = start();
        //结束时间
        String end = end();
        //根据计算出的时间查询数据库中的数据
        List<SeckillSessionEntity> start_time = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", start, end));
        if (start_time!=null&&start_time.size()>0){
            //根据场次id获取每个场次对应的商品
            List<SeckillSessionEntity> collect = start_time.stream().map(item -> {
                Long id = item.getId();
                //根据场次id查询秒杀商品信息
                List<SeckillSkuRelationEntity> entities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                item.setEntities(entities);
                return item;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    /**
     * 开始时间
     */
    public String start(){
        //获取当前的时间
        LocalDate now = LocalDate.now();
        LocalDate startDate = now.plusDays(1);//开始日期
        LocalTime min = LocalTime.MIN;//当天最小的时间
        LocalDateTime start = LocalDateTime.of(startDate, min);//开始时间
        String startTime = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return startTime;
    }

    /**
     * 结束时间
     * @return
     */
    public String end(){
        //获取当前的时间
        LocalDate now = LocalDate.now();
        LocalDate endDate = now.plusDays(2);//结束日期
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(endDate, max);//结束时间
        String format = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}